package com.xysd.express.activity.mine.setting;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.xysd.express.R;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.util.ActivityTaskManeger;
import com.xysd.express.util.JSONParser;
import com.xysd.express.util.LoadingDialogUtils;
import com.xysd.express.util.StringUtils;
import com.xysd.express.util.ToastUtil;
import com.xysd.express.util.http.HttpCallBack;
import com.xysd.express.util.http.HttpUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.Timer;
import java.util.TimerTask;

@ContentView(R.layout.activity_bind_mobile)
public class BindMobileActivity extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.mobile_et)
    private EditText mobile_et;
    @ViewInject(R.id.delete_img)
    private ImageView delete_img;
    @ViewInject(R.id.getCode_tv)
    private TextView getCode_tv;
    @ViewInject(R.id.bind_btn)
    private Button bind_btn;

    private String mobile,vertifyCode;
    @Override
    protected void init() {
        setTitleBar("绑定手机号");
        delete_img.setOnClickListener(this);
        getCode_tv.setOnClickListener(this);
        bind_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.delete_img:
                mobile_et.setText("");
                break;
            case R.id.getCode_tv:
                mobile=mobile_et.getText().toString();
                if (StringUtils.isMobileNO(mobile)){
                    getCode();
                }else {
                    ToastUtil.show("手机号格式不正确");
                }
                break;
            case R.id.bind_btn:
                vertifyCode=getCode_tv.getText().toString();
                bind();
                break;
        }
    }

    private void bind() {
        LoadingDialogUtils.createLoadingDialog(mActivity).show();
        HttpUtil.bindMobile(mobile, vertifyCode, new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                LoadingDialogUtils.closeLoadingDialog();
                Log.i("绑定手机号",result);
                String code= JSONParser.getStringFromJsonString("Status",result);
                String msg= JSONParser.getStringFromJsonString("Msg",result);
                if ("0".equals(code)){
                    ToastUtil.show("绑定成功");
                    ActivityTaskManeger.getInstance().closeActivity(mActivity);
                }else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("绑定手机号",error);
            }
        });
    }

    private void getCode() {
        HttpUtil.getVertifyCodeBind(mobile, new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                Log.i("绑定验证码",result);
                String code= JSONParser.getStringFromJsonString("Status",result);
                String msg= JSONParser.getStringFromJsonString("Msg",result);
                if ("0".equals(code)){
                    countdown(60);
                    ToastUtil.show("获取验证码成功");
                }else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("绑定验证码",error);
            }
        });
    }

    int recLen=60;
    Timer timer;
    //倒计时
    private void countdown(final int time) {
        recLen=time;
        timer=new Timer();
        timer.schedule(new TimerTask() {//因为timer和timertask均只可调用一次，所以写在内部不用全局变量
            @Override
            public void run() {

                runOnUiThread(new Runnable() {      // UI thread
                    @Override
                    public void run() {
                        recLen--;
                        getCode_tv.setText(""+recLen);
                        if(recLen < 0){
                            timer.cancel();
                            getCode_tv.setText("重新获取");
                            recLen = time;
                            getCode_tv.setClickable(true);
                        }
                    }
                });
            }
        },1000,1000);//第二参数为延迟，第三参数为执行task的run方法间隔时间，即多久变一次，单位均为毫秒
        getCode_tv.setClickable(false);
    }
}
